
$().ready(function() {
	validateRule();
	// validateRule2();
});
var fileType=0;
$.validator.setDefaults({
	submitHandler : function() {
		console.log('validator');
		var client = new OSS.Wrapper({
			accessKeyId: alists.accessKeyId,
			accessKeySecret: alists.accessKeySecret,
			stsToken: alists.securityToken,
			endpoint: alists.endPoint,
			bucket: alists.bucketName
		});

		var fileCheck=nullCheck($("#file1").get(0).value);
		var file2Check=nullCheck($("#file2").get(0).value);
		var count=0;
		if (fileCheck.right) {
			count++;
			updateFile(client,1)
		}if (file2Check.right) {
			count++;
			updateFile(client,2)
		}

		if (!nullCheck($("#pictureUrl1").val()).right) {
			parent.layer.msg("主队图片不能为空");
		}else if (!nullCheck($("#pictureUrl2").val()).right) {
			parent.layer.msg("客队图片不能为空");
		}else{
			var interval =setInterval(function(){
				if (fileType==count){
					update();
					clearInterval(interval);
				}
			},500)
		}



		// var timestamp = Date.parse(new Date());
		// var file=$("#file").get(0);
		// var picPath = file.value;
		// var suffix=picPath.substring(picPath.lastIndexOf("."), picPath.length);
		// var fileName=timestamp+"01"+suffix;
		// //storeAs表示上传的object name , file表示上传的文件
		// client.multipartUpload(fileName, file.files[0]).then(function (result) {
		// 	$("#pictureUrl").val(ossPath+fileName);
		// 	update();
		// }).catch(function (err) {
		// 	parent.layer.msg("上传失败");
		//
		// });
	}
});

function updateFile(client,num) {
	var timestamp = Date.parse(new Date());
	var file=$("#file"+num).get(0);
	var picPath = file.value;
	var suffix=picPath.substring(picPath.lastIndexOf("."), picPath.length);
	var fileName=timestamp+"02"+Math.ceil(Math.random()*10);+suffix;
	$("#pictureUrl"+num).val(ossPath+fileName);
	//storeAs表示上传的object name , file表示上传的文件
	client.multipartUpload(fileName, file.files[0]).then(function (result) {
		// $("#pictureUrl"+num).val(ossPath+fileName);
		// update();
		fileType++;
	}).catch(function (err) {
		fileType++;
		parent.layer.msg("上传失败");
	});
}
function update() {
	$.ajax({
		cache : true,
		type : "POST",
		url : "/recommend",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.resultCode == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.msg(data.resultDesc)
			}

		}
	});

}
function validateRule() {
	console.log('validateRule')

	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			name : {
				required : true,
				nameCheck :  $("#name").val()
			},
			eventsTime: {
				required : true
			},
			homeTeam: {
				required : true,
				nameCheck :  $("#homeTeam").val()
			},
			visitingTeam: {
				required : true,
				nameCheck :  $("#visitingTeam").val()
			},
			pictureUrl1: {
				required : true,
				nullCheck:  $("#pictureUrl1").val()

			},
			pictureUrl2: {
				required : true,
				nullCheck:  $("#pictureUrl2").val()
			},
			recommend: {
				required : true,
				nameCheck2 :  $("#recommend").val()
			},
			forecast: {
				required: true,    //要求输入不能为空
				number: true,     //输入必须是数字
				min: 0.01,          //输入的数字最小值为0.01，不能为0或者负数
				minNumber: $("#forecast").val(),    //调用自定义验证
				// maxNumber: $("#forecast").val()    //调用自定义验证

			},
			recommendReason: {
				required: true,    //要求输入不能为空
			},
			price: {
				required: true,    //要求输入不能为空
				number: true,     //输入必须是数字
				min: 0.01,          //输入的数字最小值为0.01，不能为0或者负数
				minNumber: $("#price").val(),    //调用自定义验证
				// maxNumber: $("#forecast").val()    //调用自定义验证
			}
		},
		messages : {
			name : {
				required : icon + "请输入赛事名称"
			},
			eventsTime : {
				required : icon + "请输入赛事时间"
			},
			homeTeam: {
				required : icon + "请输入主队名称"
			},
			visitingTeam: {
				required : icon + "请输入客队名称"
			},
			homeTeamImg: {
				required : icon + "请输入主队图片"
			},
			visitingTeamImg: {
				required : icon + "请输入客队图片"
			},
			recommend: {
				required : icon + "请输入推荐选项"
			},
			recommendReason : {
				required : icon + "请输入推荐理由"
			}
		}
	})
}


$.validator.addMethod("minNumber",function(value, element){
	var returnVal = true;
	inputZ=value;
	var ArrMen= inputZ.split(".");    //截取字符串
	if(ArrMen.length==2){
		if(ArrMen[1].length>2){    //判断小数点后面的字符串长度
			returnVal = false;
			return false;
		}
	}
	return returnVal;
},"小数点后最多为两位");
$.validator.addMethod("maxNumber",function(value, element){
	var returnVal = true;
	if(value>9999.99){
			return false;
	}
	return returnVal;
},"小数点后最多为两位");

$.validator.addMethod("nameCheck",function(value, element){
	var check=stringSubCheck(value)
	return check.right;
},"只能包括中文字、英文字母、数字！");

$.validator.addMethod("nameCheck2",function(value, element){
	var check=stringSubCheck3(value)
	return check.right;
},"只能包括中文字、英文字母、数字！");

$.validator.addMethod("nullCheck",function(value, element){
	console.log(value);
	return nullCheck(value).right;
},"小数点后最多为两位");

$.validator.addMethod("nullCheck2",function(value, element){
	if (type!=2)
		return true;
	return nullCheck(value).right;
},"不能为空");
